<?php

require_once 'FXMLEditor.class.php';

class FUtil
{
    public function __construct()
    {
    }
    
    public static function validateCep($cep)
    {
        return strlen($cep) == 8 && is_int($cep);
    }
    
    public static function validateData($data)
    {
        $parseData = date_parse($data);
        
        return checkdate($parseData['month'], $parseData['day'], $parseData['year']);
    }
    
    public static function isBoolean($value)
    {
        $true = array('t', '1', 'true', 'True');
        
        return is_bool($value) || in_array($value, $true);
    }

    public static function debug($element, $file = 'debug.log')
    {
        $conf = new FXMLEditor();
        $log = fopen($conf->getValue('dir.root') . "/logs/$file", "a");
        
        if ( is_array($element) )
        {
            foreach($element as $i => $el)
            {
                if ( is_array($el) )
                {
                    foreach($el as $j => $e)
                    {
                        fwrite($log, '[' . $i . '] => [' . $j . '] => ' . $e . "\n");
                    }
                }
                else
                {
                    fwrite($log, '[' . $i . '] => ' . $el . "\n");
                }
            }
        }
        elseif ( is_object($element) )
        {
            // TODO
        }
        elseif ( is_bool($element) )
        {
            fwrite($log, $element ? "TRUE\n" : "FALSE\n");
        }
        else
        {
            fwrite($log, $element . "\n");
        }
        
        fclose($log);
    }

    public static function log($element, $file = 'sql.log')
    {
        $conf = new FXMLEditor();
        $log = fopen($conf->getValue('dir.root') . "/logs/$file", "a");
        fwrite($log, $element . "\n");
        fclose($log);
    }
    
    public static function getScreenWidth()
    {
        $width = explode(':', $_COOKIE['ext-width']);
        
        return $width[1];
    }
    
    public static function getScreenHeight()
    {
        $height = explode(':', $_COOKIE['ext-height']);
        
        return $height[1];
    }
    
    /**
     * @todo TRANSLATION
     * Retorna o valor booleano da variável
     * Função utilizada para testar se uma variável tem um valor booleano, conforme definição: será verdadeiro de 
     *      for 1, t ou true... caso contrário será falso.
     *
     * @param mixed $value valor a ser testado
     *
     * @return boolean value
     *
     */
    public static function getBooleanValue($value)
    {
        $trues = array( 't', '1', 'true', 'True' );

        if ( is_bool($value) )
        {
            return $value;
        }

        return in_array($value, $trues);
    }
    
    public static function getReloadPageAction($waitTime = 0)
    {
        return "
            sleep($waitTime);
            window.location.reload();
        ";
    }
    
    public static function getFormSubmitAction($formName)
    {
        return "_submit(fform_{$formName});";
    }
    
    public static function getFormCloseAction()
    {
        return "_close";
    }
    
    public static function loadClasses($path = '')
    {
        // Adicionar os arquivos PHP.
        $pathPhp[] = "classes/";
        $pathPhp[] = "classes/database/";
        $pathPhp[] = "classes/widgets/";
        $pathPhp[] = "controllers/";
        $pathPhp[] = "models/";
        $pathPhp[] = "views/forms/";
        foreach ( $pathPhp as $p )
        {
            $p = $path . $p;
            $dir = dir($p);
            while ( $file = $dir->read() )
            {
                if ( $file != "." && $file != ".." && $file != ".svn" )
                {
                    if ( !is_dir($p . $file) )
                    {
                        require_once $p . $file;
                    }
                }
            }
            $dir->close();
        }
    }
}

?>
